package com.polaris.live.utils.zego

import android.view.TextureView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.polaris.live.utils.zego.listener.OnLivePlayListener

/**
 * 直播流播放器，列表中需要自行管理
 *
 * 如果调用了[startLive]，必须调用[stopLive]，否则会发生内存泄漏
 *
 * @author Created by lucas on 2023/10/17 19:27
 * @since 1.0.0
 */
class LivePlayerLifecycle(
    private var face: LiveActivityIdentifier,
    private var playMute: Boolean = true,
    private var view: TextureView,
    private val listener: OnLivePlayListener,
    private var streamId: String?,
) {

    fun observe(owner: LifecycleOwner) {
        if (owner.lifecycle.currentState == Lifecycle.State.DESTROYED) {
            return
        }

        owner.lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                when (event) {
                    Lifecycle.Event.ON_START -> {
                        streamId?.also {
                            LivePlayer.startLive(
                                it, view,
                                face, playMute, listener
                            )
                        }
                    }

                    Lifecycle.Event.ON_STOP -> {
                        LivePlayer.stopLive(view)
                    }

                    Lifecycle.Event.ON_DESTROY -> {
                        LivePlayer.stopLive(view)
                    }

                    else -> {
                        //ignore
                    }
                }
            }
        })
    }

    fun getNowPlayStreamId(): String? {
        return streamId
    }

    /**
     * 流是否在播放
     */
    fun isStreamIdPaly(streamId: String): Boolean {
        return LivePlayer.isStreamIdPaly(streamId)
    }

    /**
     * 开始播放
     */
    fun startStream(owner: LifecycleOwner, streamId: String) {
        if (owner.lifecycle.currentState == Lifecycle.State.RESUMED) {
            LivePlayer.startLive(
                streamId, view,
                face, playMute, listener
            )
        }
        this.streamId = streamId
    }

    /**
     * 停止播放
     */
    fun stopStream() {
        LivePlayer.stopLive(view)
        streamId = null
    }

    fun switchStream(owner: LifecycleOwner, streamId: String) {
        if (owner.lifecycle.currentState == Lifecycle.State.RESUMED) {
            LivePlayer.startLive(
                streamId, view,
                face, playMute, listener
            )
        }

        this.streamId = streamId
    }

    companion object {

        /**
         * 订阅直播流
         *
         * @param owner 生命周期
         * @param view 播放view
         * @param playMute 是否静音
         * @param listener 播放监听
         */
        fun create(
            face: LiveActivityIdentifier,
            owner: LifecycleOwner,
            view: TextureView,
            playMute: Boolean = true,
            listener: OnLivePlayListener,
        ): LivePlayerLifecycle {
            return LivePlayerLifecycle(face, playMute, view, listener, null).apply {
                observe(owner)
            }
        }

        /**
         * 订阅直播流
         *
         * @param owner 生命周期
         * @param view 播放view
         * @param streamId 直播流id
         * @param playMute 是否静音
         * @param listener 播放监听
         */
        fun observe(
            face: LiveActivityIdentifier,
            owner: LifecycleOwner,
            view: TextureView,
            streamId: String,
            playMute: Boolean = true,
            listener: OnLivePlayListener,
        ) {
            LivePlayerLifecycle(face, playMute, view, listener, streamId).observe(owner)
        }
    }
}
